/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package admincommands;

import gameserver.configs.AdminConfig;
import gameserver.dao.PlayerDAO;
import gameserver.model.gameobjects.player.Player;
import gameserver.network.loginserver.BannedMacManager;
import gameserver.utils.PacketSendUtility;
import gameserver.utils.ThreadPoolManager;
import gameserver.utils.chathandlers.AdminCommand;
import gameserver.utils.i18n.CustomMessageId;
import gameserver.utils.i18n.LanguageHandler;
import gameserver.utils.Util;

import commons.database.dao.DAOManager;

public class UnBanMac extends AdminCommand
{
	public UnBanMac()
	{
		super("unbanmac");
	}
	
	@Override
	public void executeCommand(final Player admin, String[] params)
	{
		if(admin.getAccessLevel() < AdminConfig.COMMAND_BANMAC)
		{
			PacketSendUtility.sendMessage(admin, LanguageHandler.translate(CustomMessageId.COMMAND_NOT_ENOUGH_RIGHTS));
			return;
		}
		
		if (params.length <= 0)
		{
			PacketSendUtility.sendMessage(admin, "Syntax: //unbanmac <player>");
			return;
		}
		
		String name = Util.convertName(params[0]);
		final int playerId = DAOManager.getDAO(PlayerDAO.class).getPlayerIdByName(name);
		
		if(playerId > 0)
		BannedMacManager.getInstance().getMacAddress(playerId);
		
		ThreadPoolManager.getInstance().schedule(new Runnable()
		{
			@Override
			public void run()
			{
				String macAddress = BannedMacManager.getInstance().loadMacAddress();
				
				if(macAddress != null)
				{
					boolean result = BannedMacManager.getInstance().unbanAddress(macAddress,playerId, "uban;mac=" + macAddress + ", playerId : " + playerId + "; admin=" + admin.getName());
					if (result)
						PacketSendUtility.sendMessage(admin, "mac " + macAddress + " has unbanned");
					else
						PacketSendUtility.sendMessage(admin, "mac " + macAddress + " is not banned");
				}
				else
				{
					PacketSendUtility.sendMessage(admin, "Mac Address was not found!");
				}
			}
		}, 1000);
	}
}
